//
//  LZTextViewTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/20.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZTextViewTableViewCell.h"
#import "Masonry.h"
#import <objc/runtime.h>
#import <objc/message.h>

#import "LZCouponListModel.h"
#import "LZAlertView.h"

@interface LZTextViewTableViewCell() <UITextViewDelegate>
@property (nonatomic, strong) UILabel * titleLabel;
@property (nonatomic, strong) UITextView * textView;
@property (nonatomic, strong) UILabel * placeHolderLabel;
@property (nonatomic, strong) UIView * line;
@end

@implementation LZTextViewTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)configView {
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.textView];
    [self.textView addSubview:self.placeHolderLabel];
    [self.textView setValue:self.placeHolderLabel forKey:@"_placeholderLabel"];
    [self.contentView addSubview:self.line];
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(20);
    }];
    
    [self.textView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.titleLabel.mas_bottom).with.offset(10);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.bottom.mas_equalTo(-1);
    }];

    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0.5);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.6);
    }];
}

- (UILabel *)titleLabel {
    
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = UIColorFromRGB(0x333333, 1);
    }
    
    return _titleLabel;
}

- (UITextView *)textView {

    if (_textView == nil) {
        _textView = [[UITextView alloc] initWithFrame:CGRectZero];
        _textView.textColor = UIColorFromRGB(0x333333, 1);
        _textView.tintColor = UIColorFromRGB(0x33a928, 1);
        _textView.font = [UIFont systemFontOfSize:15];
        _textView.delegate = self;
    }
    
    return _textView;
}

- (UILabel *)placeHolderLabel {

    if (_placeHolderLabel == nil) {
        _placeHolderLabel = [[UILabel alloc] init];
        _placeHolderLabel.font = [UIFont systemFontOfSize:15];
        _placeHolderLabel.numberOfLines = 0;
        _placeHolderLabel.textColor = UIColorFromRGB(0xcccccc, 1);
        [_placeHolderLabel sizeToFit];
    }
    
    return _placeHolderLabel;
}

- (UIView *)line {
    
    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (self.textViewEditDidEndBlock) {
        self.textViewEditDidEndBlock(textView.text, textView.tag, self.indexPath);
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([LZAlertViewManager shareInstance].isShowAlert) {
        [textView resignFirstResponder];
    }
}

- (void)setTextViewTag:(NSInteger)tag {
    self.textView.tag = tag;
}

- (void)setCouponListModel:(LZCouponListModel *)couponListModel {

    _couponListModel = couponListModel;
    self.textView.keyboardType = UIKeyboardTypeDefault;
    
    switch (self.textView.tag) {
        case 301:{ //食材
        
            self.titleLabel.text = LZLocalizedString(@"ingredients_components", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_enter_ingredients_components", nil);
            self.textView.text = couponListModel.promotionFood;
        }
            break;
        case 302:{ //标签
        
            self.titleLabel.text = LZLocalizedString(@"tags", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_enter_tags", nil);
            self.textView.text = couponListModel.tag;
        }
            break;
            
            //优惠劵
            
        case 100:{ //优惠券名称
        
            self.titleLabel.text = LZLocalizedString(@"name", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_enter_coupon_name", nil);
            self.textView.text = couponListModel.couponsName;
        }
            break;
        case 102:{ //优惠劵使用条件
            
            self.titleLabel.text = LZLocalizedString(@"conditions", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"coupon_valid_conditions", nil);
            self.textView.text = couponListModel.couponsUseConditions;
        }
            break;
        case 103:{ //优惠劵数量
       
            self.textView.keyboardType = UIKeyboardTypeNumberPad;
            self.titleLabel.text = LZLocalizedString(@"coupons_to_issue", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_enter_coupons_to_issue", nil);
            
            if (couponListModel.couponsTotalCount > 0) {
                self.textView.text = [NSString stringWithFormat:@"%ld", (long)couponListModel.couponsTotalCount];
            }
            else {
                self.textView.text = @"";
            }
        }
            break;
        case 105: {
            
            self.titleLabel.text = LZLocalizedString(@"coupon_not_valid_on", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_select_the_date", nil);
            self.textView.text = couponListModel.couponsExceptTime;
        }
            break;
        case 107: {
            
            self.titleLabel.text = LZLocalizedString(@"need_to_book_in_advance", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_need_to_book", nil);
            self.textView.text = couponListModel.couponsRemindAppoint;
        }
            break;
        case 108: {
            
            self.titleLabel.text = LZLocalizedString(@"other_conditions", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_other_conditions", nil);
            self.textView.text = couponListModel.couponsRemindRule;
        }
            break;
            
            //团购
        case 200: { //团购名
            
            self.titleLabel.text = LZLocalizedString(@"name", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_enter_group_name", nil);
            self.textView.text = couponListModel.grouponName;
        }
            break;
        case 204: {
            
            self.textView.keyboardType = UIKeyboardTypeNumberPad;
            self.titleLabel.text = LZLocalizedString(@"required_people", @"");
            self.placeHolderLabel.text = LZLocalizedString(@"please_enter_group_number_people", nil);
            
            if (couponListModel.grouponCount > 0) {
                self.textView.text = [NSString stringWithFormat:@"%ld", (long)couponListModel.grouponCount];
            }
            else {
                self.textView.text = @"";
            }
        }
            break;
        case 207: {
            
            self.titleLabel.text = LZLocalizedString(@"coupon_not_valid_on", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_select_the_date", nil);
            self.textView.text = couponListModel.grouponExceptTime;
        }
            break;
        case 209: {
            
            self.titleLabel.text = LZLocalizedString(@"need_to_book_in_advance", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_need_to_book", nil);
            self.textView.text = couponListModel.grouponRemindAppoint;
        }
            break;
        case 210: {
            
            self.titleLabel.text = LZLocalizedString(@"other_conditions", nil);
            self.placeHolderLabel.text = LZLocalizedString(@"please_other_conditions", nil);
            self.textView.text = couponListModel.grouponRemindRule;
        }
            break;
        default:
            break;
    }
    
}

#pragma mark - textViewDelegate

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    if (textView == self.textView) {
        
        NSInteger existedLength = textView.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = text.length;
        
        if (existedLength - selectedLength + replaceLength > 1000) {
            return NO;
        }
        
        NSInteger textlength = existedLength - selectedLength + replaceLength;
        
        switch (textView.tag) {
            case 301: //食材
            {
                if (textlength > 100) {
                    return NO;
                }
            }
                break;
            case 302: //标签
            {
                if (textlength > 100) {
                    return NO;
                }
            }
                break;
            case 100: //优惠卷名
            {
                if (textlength > 50) {
                    return NO;
                }
            }
                break;
            case 102: //优惠劵使用条件
            {
                if (textlength > 100) {
                    return NO;
                }
            }
                break;
            case 103: //优惠劵数量
            {
                if (textlength > 4) {
                    return NO;
                }
            }
                break;
            case 105: //优惠劵数量
            {
                if (textlength > 100) {
                    return NO;
                }
            }
                break;
            case 107:
            {
                if (textlength > 1000) {
                    return NO;
                }
            }
                break;
            case 108:
            {
                if (textlength > 1000) {
                    return NO;
                }
            }
                break;
            case 200: //团购名
            {
                if (textlength > 50) {
                    return NO;
                }
            }
                break;
            case 204: //成团人数
            {
                if (textlength > 3) {
                    return NO;
                }
            }
                break;
            case 207: //除外日期
            {
                if (textlength > 100) {
                    return NO;
                }
            }
                break;
            case 209: //预约
            {
                if (textlength > 1000) {
                    return NO;
                }
            }
                break;
            case 210: //规则
            {
                if (textlength > 1000) {
                    return NO;
                }
            }
                break;
            default:
                break;
        }
    }
    
    return YES;
}

+ (CGFloat)getCellHeightWithText:(NSString *)text {
    if (text) {
        NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15.]};
        CGRect titleRect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-30, MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];
        return 15+20+15+titleRect.size.height+15;
    }
    else {
        return 15+20+15+20+15;
    }
}
@end
